package edu.wong.controller;

import edu.wong.entities.PayDTO;
import edu.wong.resp.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

@RestController
public class OrderController {
//    public static final String PaymentSrv_URL = "http://localhost:8001";
    public static final String PaymentSrv_URL = "http://cloud-payment-service";

    @Resource
    private RestTemplate restTemplate;

    //使用httpclient调用pay模块的相关接口
    @GetMapping("/consumer/pay/add")
    public ResultData addOrder(PayDTO payDTO) throws IOException {
        return restTemplate.postForObject(PaymentSrv_URL + "/pay/add", payDTO, ResultData.class);
    }

    @GetMapping("/consumer/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id) throws IOException {
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/"+id,ResultData.class,id);
    }

    @DeleteMapping("consumer/pay/del/{id}")
    public ResultData deleteOrder(@PathVariable("id") Integer id) {
         restTemplate.delete(PaymentSrv_URL + "/pay/del/"+id,ResultData.class,id);
         return ResultData.success("success");
    }

    @PutMapping( "consumer/pay/update")
    public ResultData updateOrder(@RequestBody PayDTO payDTO) {
        restTemplate.put(PaymentSrv_URL+"/pay/update",payDTO,ResultData.class);
        return ResultData.success("success");

    }

    @GetMapping(value = "consumer/pay/getAll")
    @Operation(summary = "获得全部",description = "获得数据库中的全部内容")
    public ResultData getAll(){
        return restTemplate.getForObject(PaymentSrv_URL+"/pay/getAll",ResultData.class);
    }

    @GetMapping(value = "consumer/pay/get/info")
    public String getInfoByConsul(){
        return restTemplate.getForObject(PaymentSrv_URL+"/pay/get/info",String.class);
    }

}
